﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Configuration;
using DevExpress.XtraNavBar;
using DevExpress.XtraEditors;
using Share;
using SLMNG.DialogForm;
using System.Threading;
using BUS;
using Utilities;

namespace SLMNG
{
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        dlgProgressCircle dlgProg;        

        public FrmMain()
        {
            InitializeComponent();

            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);
            DevExpress.Skins.SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.OfficeSkins).Assembly);
            btnAdd.ItemClick += btnAdd_ItemClick;
            btnEdit.ItemClick += btnEdit_ItemClick;
            btnSave.ItemClick += btnSave_ItemClick;
            btnDelete.ItemClick += btnDelete_ItemClick;
            btnPrint.ItemClick += btnPrint_ItemClick;
            btnRefresh.ItemClick += btnRefesh_ItemClick;
            narBarControl.LinkPressed += narBarControl_LinkPressed;

            dlgProg = new dlgProgressCircle();
            dlgProg.StartPosition = FormStartPosition.CenterParent;
        }

        void narBarControl_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            NavBarItem obj = ((NavBarControl)sender).PressedLink.Item;
            switch (obj.Name)
            {
                case "DMNL":
                    OpenTabPage(obj.Name, new CA_Materials());
                    break;
                case "DMMA":
                    OpenTabPage(obj.Name, new CA_Foods());
                    break;
                case "DMKV":
                    OpenTabPage(obj.Name, new CA_Zones());
                    break;
                case "DMBA":
                    OpenTabPage(obj.Name, new CA_Tables());
                    break;
            }            
        }        

        #region Tool Bar button

        void btnAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    BaseForm frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncInsert();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        

        void btnRefesh_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    BaseForm frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncRefresh();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    BaseForm frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncPrint();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    BaseForm frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncDelete();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    BaseForm frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncSave();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btnEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (tabMain.TabPages.Count > 0)
                {
                    BaseForm frm = (BaseForm)tabMain.SelectedTabPage.Controls[0];
                    if (frm != null)
                    {
                        frm.fncEdit();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Form Event

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            string currentSkinName = "Summer 2008";
            //ConfigurationManager.AppSettings["SkinName"];
            for (int i = 0; i < DevExpress.Skins.SkinManager.Default.Skins.Count; i++)
                comboBoxEdit1.Properties.Items.Add(DevExpress.Skins.SkinManager.Default.Skins[i].SkinName);
            comboBoxEdit1.SelectedText = currentSkinName;

            lblStatusMain.Caption = Name;
            //xtraTabPage1.Text = Name;
            //xtraTabPage1.ImageIndex = 1;

            string SkinName = currentSkinName;
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(SkinName);
            lblStatusMain.Caption = string.Format("Skin list ({0} skin)", comboBoxEdit1.Properties.Items.Count.ToString());

            comboBoxEdit1.SelectedIndexChanged += new EventHandler(comboBoxEdit1_SelectedIndexChanged);
            barStaticItem2.Caption = Global.UserID;

            CheckConnection();
            
        }

        public void CheckConnection()
        {
            try
            {
                //if (CatalogueBase.CheckConnection())
                //{
                //    MessageBox.Show("Kết nối tới CSDL thành công!");
                //}
                //else
                //{
                //    MessageBox.Show("Kết nối tới CSDL thất bại!");
                //}

                //ErpMultiLanguage.Refresh();

                //DialogResult dlg = ErpMessageBoxEX.Show("Chay duoc roi ne", this.Text, EnumMessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            }
            catch
            {
            }
        }

        void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string NewSkinName = comboBoxEdit1.SelectedText;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //config.AppSettings.Settings["SkinName"].Value = NewSkinName;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSetting");
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(NewSkinName);
            barStaticItem1.Caption = "Đã lưu.";
        }

        #endregion

        #region Item Click

        private void navBarItem2_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            OpenTabPage(((NavBarItem)sender).Name, new FrmControl());
        }

        private void OpenTabPage(string tabName, BaseForm frm)
        {
            int index = CheckExists(tabName);
            if (index < 0)
            {
                DevExpress.XtraTab.XtraTabPage tabPage = new DevExpress.XtraTab.XtraTabPage();
                tabPage.Name = tabName;
                tabPage.Text = frm.Text;
                tabMain.TabPages.Add(tabPage);

                //Form1 frm = new Form1();
                frm.ControlBox = false;
                frm.TopLevel = false;
                frm.Parent = tabPage;
                frm.Show();
                frm.Dock = DockStyle.Fill;
                frm.StatusBarTextChanged += new StatusBarEventHandler(actionForm_StatusBarTextChanged);

                tabMain.SelectedTabPageIndex = tabMain.TabPages.Count - 1;
                
            }
            else
            {
                tabMain.SelectedTabPageIndex = index;
            }
        }

        private void actionForm_StatusBarTextChanged(object sender, StatusBarEventArgs e)
        {
            lblStatusMain.Caption = e.StatusText;
            if (e.StatusText == Messages.STATUS_INSERT || e.StatusText == Messages.STATUS_LOADING || e.StatusText == Messages.STATUS_UPDATE)
            {
                //dlgProg.TopMost = true;
                //dlgProg.Show();
                prbMain.Visibility = BarItemVisibility.Always;
            }
            else
            {
                dlgProg.Hide();
                prbMain.Visibility = BarItemVisibility.Never;
            }
        }

        private int CheckExists(string tabName)
        {
            for (int i = 0; i < tabMain.TabPages.Count; i++)
            {
                if (tabMain.TabPages[i].Name == tabName)
                {
                    return i;
                }
            }

            return -1;
        }

        private void nbiServiceType_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            OpenTabPage(((NavBarItem)sender).Name, new CA_Tables());
        }

        private void tabMain_CloseButtonClick(object sender, EventArgs e)
        {
            tabMain.SelectedTabPage.Dispose();
        }

        private void navBarItem3_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            OpenTabPage(((NavBarItem)sender).Name, new CA_Zones());
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void nbiNhapHang_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            OpenTabPage(((NavBarItem)sender).Name, new M_frmPurchase());
        }

        //private void nbiAutoNumber_LinkClicked(object sender, NavBarLinkEventArgs e)
        //{
        //    OpenTabPage(((NavBarItem)sender).Name, new AutoNumber());
        //}

        private void ribbonControl1_MouseDown(object sender, MouseEventArgs e)
        {
            int rs = e.Button.CompareTo(MouseButtons.Right);
            if (e.Button.CompareTo(MouseButtons.Right) == 0)
            {

            }
        }

        private void navBarControl1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                NavBarItemLink item = narBarControl.PressedLink;
                switch (item.ItemName)
                {
                    case "nbiAutoNumber":
                        OpenTabPage(item.ItemName, new FrmAutoNumber());
                        break;
                }
            }
            catch
            {
            }
        }

        #endregion

        #region Narbar Control Clicked

        private void nbiOrder_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            OpenTabPage(((NavBarItem)sender).Name, new M_Invoices());
        }

        private void nbiPayment_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            OpenTabPage(((NavBarItem)sender).Name, new M_Payments());
        }

        #endregion
    }
}